﻿using System;
using BusinessBingo.CommandHandlers.Base;
using BusinessBingo.CommandHandlers.Interfaces;
using BusinessBingo.CommandHandlers.Results;
using BusinessBingo.Commands;
using BusinessBingo.Data.Interfaces;
using BusinessBingo.Model;
using BusinessBingo.Model.Interfaces;

namespace BusinessBingo.CommandHandlers
{
    public class CheckWordCommandHandler :
        BaseCommandHandler,
        ICheckWordCommandHandler
    {
        private IBingoSetRepository _bingoSetRepository;

        public CheckWordCommandHandler(
            IBingoSetRepository bingoSetRepository,
            IValidationState validationState,
            IUnitOfWork unitOfWork) :
            base(validationState, unitOfWork)
        {
            this._bingoSetRepository = bingoSetRepository;
        }

        public CheckWordCommandHandlerResult Execute(CheckWordCommand checkWordCommand)
        {
            CheckWordCommandHandlerResult result = new CheckWordCommandHandlerResult();

            if (checkWordCommand == default(CheckWordCommand))
                throw new ArgumentNullException("CheckWordCommand");
            if (checkWordCommand.UserId == default(Guid))
                this._validationState.AddError<CheckWordCommand>(x => x.UserId, ErrorTypes.Empty);
            if (checkWordCommand.WordId == default(Guid))
                this._validationState.AddError<CheckWordCommand>(x => x.WordId, ErrorTypes.Empty);

            if (this._validationState.IsValid == false)
                return result;

            this._bingoSetRepository.CheckWord(checkWordCommand.UserId, checkWordCommand.GameId, checkWordCommand.WordId);

            base.Complete();

            result.Succeeded = true;
            return result;
        }
    }
}